//
//  DDLDCompanyIntroduceCell.swift
//  DingDongZP
//
//  Created by 杨政涛 on 2016/12/27.
//  Copyright © 2016年 51ledian.com. All rights reserved.
//

import UIKit

class DDLDCompanyIntroduceCell: LDBaseRecFinderCell {


    let margin_V = LDMargin_V //竖直间隔
    let BtnHeight:CGFloat = 15
    
    var companyIntroduceTitle:UILabel?
    var companyDesc:UILabel?
    var divideLineView2 = UIImageView.setupDivideLine_H(LDFrame.ScreenWidth - Margin_V*10)
    
    var detailsBtn:UIButton!
    
    var models = DDCompanyInfo() {
        didSet{
            
            updateContentSub()
        }
    }
    
    var tapMoreIntroduceBlock:((cell:DDLDCompanyIntroduceCell)->())?
    //frame
    private let btnMargin:CGFloat = 10
    
    //MARK: - OVERRIDE
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = UIColor.whiteColor()
        //布局
        setDefaultContent()
        setupFrame()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func updateContentSub(){
        
        let publishModel:DDCompanyInfo = self.models
        
        let attr = String.getAttrFormString(lineH: 8, text: publishModel.company_intro ?? "公司介绍", font: LDFont.Font12)
        companyDesc?.attributedText = attr
        
        companyDesc!.snp_updateConstraints(closure: { (make) in
            
            make.height.equalTo(models.companyDescH)
        })
    
    }
    
    
    // MARK: - init 布局
    //添加view到主视图
    func setDefaultContent(){
        
        companyIntroduceTitle = UILabel.setDetailsLab("公司介绍", fontSize: 13, titleColor: LDGolbalColor.GreenAssist, Alignment: .Left)
        companyDesc = UILabel()
        companyDesc?.numberOfLines = 0
        
        //分割线--------
        
        let btn = UIButton.setDetailsBtn("展开", picName: "more_detail_btn", btnBackColor: nil, titleColor: LDFontColor.Green)
        btn?.setImage(UIImage(named: "more_detail_btn_up"), forState: UIControlState.Selected)
        btn?.setTitle("收起", forState: UIControlState.Selected)
        
        detailsBtn = UIButton.setButtonImageToRight(btn!)
        
        
        detailsBtn.rac_signalForControlEvents(UIControlEvents.TouchUpInside).subscribeNext { [weak self] (obj) in
            
            
            if self == nil {
                return
            }
            
            self!.detailsBtn.selected = !self!.detailsBtn.selected
            if self?.tapMoreIntroduceBlock != nil {
                
                self?.tapMoreIntroduceBlock!(cell: self!)
            }
        }
        
        contentView.addSubview(companyIntroduceTitle!)
        contentView.addSubview(companyDesc!)
        
        contentView.addSubview(divideLineView2)
        addSubview(detailsBtn)
        
    }
    
    //MARK - 自动布局
    func setupFrame(){
        
        companyIntroduceTitle?.snp_makeConstraints(closure: { (make) in
            
            make.top.equalTo(20)
            make.left.equalTo(15)
            make.height.equalTo(15)
            
        })
        
        companyDesc?.snp_makeConstraints(closure: { (make) in
            
            make.top.equalTo(companyIntroduceTitle!.snp_bottom).offset(margin_V*1.5)
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.height.equalTo(5)
            //            make.bottom.equalTo(divideLineView2.snp_top)
            
        })
        
        //分割线
        divideLineView2.snp_makeConstraints { (make) in
            
            make.top.equalTo(companyDesc!.snp_bottom).offset(margin_V)
            make.height.equalTo(1)
            make.left.equalTo(LDMargin_H*3)
            make.right.equalTo(-LDMargin_H*3)
        }
        
        detailsBtn.snp_makeConstraints(closure: { (make) in
            
            make.top.equalTo(divideLineView2.snp_bottom)
            make.left.right.equalTo(0)
            make.height.equalTo(35)
            
        })
        
    }
}
